<?php
$orderby = get_major_key(array('id' => 'DESC'));
$oOrder = new Order();
$deliverys = cache_read('deliverys.php');

if(!empty($_GET['btnExport'])){	// 导出
	
	$list = $oOrder->get_invoice_list('*', $_GET, $orderby, 1, 65536, $offset = 0);
	if(!empty($list['list']) && is_array($list['list'])){
		$data = array();
		foreach($list['list'] as $key => $row){
			$row['addrinfo']			= $oOrder->get_order_address($row['sn']);
			$row['invoice_sn']		= $row['invoice_sn'].chr(32);
			$row['sn']				= $row['sn'].chr(32);
			$row['deliveryname']	= $deliverys[$row['delivery']]['name'];
			$row['consignee']		= $row['addrinfo']['consignee'];
			$row['mobile']			= $row['addrinfo']['mobile'];
			$row['tel']				= $row['addrinfo']['tel'];
			$row['province_name']	= $row['addrinfo']['provice_name'];
			$row['city_name']		= $row['addrinfo']['city_name'];
			$row['area_name']		= $row['addrinfo']['area_name'];
			$row['zipcode']			= $row['addrinfo']['zipcode'];
			$row['address']			= $row['addrinfo']['address'];
			$row['addtime']			= ftime($row['addtime']);
			$data[] = $row;
		}
		$headers = array(
			'invoice_sn'	=> array('title' => '发货单号',		'width' => '18'),
			'sn'			=> array('title' => '订单号',		'width' => '18'),
			'username'		=> array('title' => '用户名',		'width' => '12.5'),
			'consignee'		=> array('title' => '收货人',		'width' => '12.5'),
			'deliveryname'	=> array('title' => '配送方式',		'width' => '12.5'),
			'express_no'	=> array('title' => '快递号',		'width' => '18'),
			'consignee'		=> array('title' => '收货人',		'width' => '12.5'),
			'mobile'		=> array('title' => '手机',			'width' => '12.5'),
			'tel'			=> array('title' => '固话',			'width' => '12.5'),
			'province_name'	=> array('title' => '省',			'width' => '12.5'),
			'city_name'		=> array('title' => '市',			'width' => '12.5'),
			'area_name'		=> array('title' => '区',			'width' => '12.5'),
			'address'		=> array('title' => '地址',			'width' => '25',		'wrap' => true),
			'zipcode'		=> array('title' => '邮编',			'width' => '8'),
			'addtime'		=> array('title' => '登记时间', 	'width' => '18'),
		);
		if($list['total'] > 5000){	// 导出超过5000时用CSV
			export_csv($data, $headers, '_E_发货单_'.date('Ymd_His').'.csv');
		}else{
			$oExcel = new Excel();
			$oExcel->export($headers, $data, '_E_发货单_'.date('Ymd_His').'.xlsx');
		}
	}
	exit;
}

$list = $oOrder->get_invoice_list('*', $_GET, $orderby, $__page, $__pagesize);
if(!empty($list['list']) && is_array($list['list'])){
	// 主键排序字段
	make_major_key_options(array(
		'id'		=> '发货时间',
		'orderid'	=> '下单时间',
	));

	foreach($list['list'] as $key => $row){
		// 收货人信息
		$row['addrinfo'] = $oOrder->get_order_address($row['sn']);
		// 配送信息
		$row['deliveryinfo'] = $deliverys[$row['delivery']];
		foreach($row['goodslist'] as $k => $goods){
			$goods['goodsinfo']['fweight'] = format_weight_out($goods['goodsinfo']['weight'], $goods['goodsinfo']['weightunit']);
			if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
				if(!empty($goods['goodsinfo']['filename'])){
					$goods['url'] = PUB_DIR.'goods/'.$goods['goodsinfo']['filename'].'.html';
				}else{
					$goods['url'] = PUB_DIR.'goods/'.time_to_path($goods['goodsinfo']['addtime']).'/'.$goods['goodsinfo']['id'].'.html';
				}
			}else{
				$goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['goodsinfo']['id']}";
			}
			$row['goodslist'][$k] = $goods;
		}
		$row['addtime'] = ftime($row['addtime']);
		$row['updatetime'] = ftime($row['updatetime']);
		$list['list'][$key] = $row;
	}
	$vars['list'] = $list['list'];
	$vars['pagelist'] = $list['pagelist'];
}
?>